package com.goudan.rabbitmq.config;

import com.goudan.rabbitmq.constant.QueueDefinition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 延迟队列配置类
 */
@Slf4j
@Configuration
public class   DelayedModeConfig {

    // 定义延迟队列
    @Bean
    public Queue delayedQueue() {
        return new Queue(QueueDefinition.LGD_DELAYED_QUEUE);
    }

    @Bean
    public CustomExchange delayedExchange() {
        Map<String, Object> args = new HashMap<>();
        // 自定义交换机的类型
        args.put("x-delayed-type", "direct");
        log.info("创建延迟交换机:{}", QueueDefinition.LGD_DELAYED_EXCHANGE);
        return new CustomExchange(QueueDefinition.LGD_DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }

    @Bean
    public Binding bindingDelayedQueue(@Qualifier("delayedQueue") Queue queue,
                                       @Qualifier("delayedExchange") CustomExchange delayedExchange) {
        log.info("绑定延迟交换机,RoutingKey:{}", QueueDefinition.LGD_DELAYED_ROUTING_KEY);
        return BindingBuilder.bind(queue).to(delayedExchange).with(QueueDefinition.LGD_DELAYED_ROUTING_KEY).noargs();
    }
}
